
var WebTorrent = require('webtorrent')
var client = new WebTorrent()
var aria2 = module.exports = {
    torrents :[]
};
aria2.getVersion = function(object){

    //{"id":4,"jsonrpc":"2.0","result":{"enabledFeatures":["Async DNS","BitTorrent","Firefox3 Cookie","GZip","HTTPS","Message Digest","Metalink","XML-RPC","SFTP"],"version":"1.19.0"}}
    var result = {
        "enabledFeatures":["Async DNS","BitTorrent","Firefox3 Cookie","GZip","HTTPS","Message Digest","Metalink","XML-RPC","SFTP"],
        "version":"0.01"
    };
    return aria2.getReturn(object, result);
}
//{"jsonrpc":"2.0","method":"aria2.tellStatus","id":12,"params":["db9af50f6ca92772"]}
aria2.tellStatus = function(object){

    //{"id":12,"jsonrpc":"2.0",
    // "result":{"bitfield":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    // "bittorrent":{"announceList":[["udp:\/\/tracker.yify-torrents.com\/announce"],
    // ["udp:\/\/open.demonii.com:1337"],["udp:\/\/exodus.desync.com:6969"],
    // ["udp:\/\/tracker.istole.it:80"],["udp:\/\/tracker.publicbt.com:80"],["udp:\/\/tracker.openbittorrent.com:80"],["udp:\/\/tracker.leechers-paradise.org:6969"],["udp:\/\/9.rarbg.com:2710"],["udp:\/\/p4p.arenabg.ch:1337"],["udp:\/\/p4p.arenabg.com:1337"],["udp:\/\/tracker.coppersurfer.tk:6969"]],"comment":"Made using YIFY Bot","creationDate":1444355527,"info":{"name":"Max (2015)"},"mode":"multi"},
    // "completedLength":"491520","connections":"7",
    // "dir":"D:\\electron\\aria2\\aria2-1.19.0-win-32bit-build1",
    // "downloadSpeed":"0",
    // "files":[{"completedLength":"0","index":"1","length":"856213846","path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Max (2015)\/Max.2015.720p.BluRay.x264.YIFY.mp4","selected":"true","uris":[]},
    // {"completedLength":"0","index":"2","length":"105983","path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Max (2015)\/WWW.YTS.TO.jpg","selected":"true","uris":[]}],
    // "gid":"db9af50f6ca92772","infoHash":"5d6ec7a663867174eb0cdf3900133032982f0614","numPieces":"409","numSeeders":"2",
    // "pieceLength":"2097152","status":"active",
    // "totalLength":"856319829","uploadLength":"0","uploadSpeed":"0"}}
    var torrent = client.torrents[object.params[0]];
    var info = {
        "bitfield":  torrent.bitfield.buffer.toString("hex"),
        "bittorrent": torrent.ready ? aria2._getBittorrentInfo(torrent) : {},
        "completedLength": torrent.ready ? torrent.downloaded : 0,
        "connections": torrent.ready ? torrent.numPeers : 0,
        "dir": torrent.ready ? torrent.path : "",
        "downloadSpeed": torrent.ready ? torrent.downloadSpeed() : "0",
        "files": torrent.ready ? aria2._getFiles(torrent) : [],
        "gid": object.params[0],
        "infoHash": torrent.ready ? torrent.infohash : "",
        "numPieces": torrent.ready ? torrent.pieces.length : 0, //todo
        "numSeeders": 0,
        "pieceLength": torrent.ready ? torrent.pieceLength : 0,
        "status": "active",
        "totalLength": torrent.ready ? torrent.length : 0,
        "uploadLength": torrent.ready ? torrent.uploaded : 0,
        "uploadSpeed": torrent.ready ? torrent.uploadSpeed() : 0

    };

    return aria2.getReturn(object, info);
}
aria2.tellActive = function(object){
   // {"id":1023,"jsonrpc":"2.0","result":[{"bitfield":"7873cfff9f9de9def977fb9fbefdfc6afbe9f55dffeee95ce5fec6eeb42e0fef268fd9bc7987a5f7e677fb3fdbb9fb7de2c7f0","bittorrent":{"announceList":[["udp:\/\/tracker.yify-torrents.com\/announce"],["udp:\/\/open.demonii.com:1337"],["udp:\/\/exodus.desync.com:6969"],["udp:\/\/tracker.istole.it:80"],["udp:\/\/tracker.publicbt.com:80"],["udp:\/\/tracker.openbittorrent.com:80"],["udp:\/\/tracker.leechers-paradise.org:6969"],["udp:\/\/9.rarbg.com:2710"],["udp:\/\/p4p.arenabg.ch:1337"],["udp:\/\/p4p.arenabg.com:1337"],["udp:\/\/tracker.coppersurfer.tk:6969"]],"comment":"Made using YIFY Bot","creationDate":1444268527,"info":{"name":"Dope (2015)"},"mode":"multi"},
   //
   //
   // "completedLength":"611897697","connections":"0","dir":"D:\\electron\\aria2\\aria2-1.19.0-win-32bit-build1","downloadSpeed":"0","files":[{"completedLength":"583856994","index":"1","length":"846000994","path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Dope (2015)\/Dope.2015.720p.BluRay.x264.YIFY.mp4","selected":"true","uris":[]},{"completedLength":"105983","index":"2","length":"105983","path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Dope (2015)\/WWW.YTS.TO.jpg","selected":"true","uris":[]}],"gid":"42c47a2a65383c1b","infoHash":"4bdc86d1c8f1ca0bdef51e88fe84ee6e5ee09f2d","numPieces":"404","numSeeders":"0","pieceLength":"2097152","status":"active","totalLength":"846106977","uploadLength":"161349632","uploadSpeed":"0"}]}
    //console.log(client.torrents);
    var result = [];
    var index = 0;
    client.torrents.forEach(function(torrent) {
        if (torrent.ready) {
           //console.log(torrent.bitfield.buffer);
            var info = {
                "bitfield": torrent.bitfield.buffer.toString("hex"),
                "bittorrent": torrent.ready ? aria2._getBittorrentInfo(torrent) : {},
                "completedLength": torrent.ready ? torrent.downloaded : 0,
                "connections": torrent.ready ? torrent.numPeers : 0,
                "dir": torrent.ready ? torrent.path : "",
                "downloadSpeed": torrent.ready ? torrent.downloadSpeed() : "0",
                "files": torrent.ready ? aria2._getFiles(torrent) : [],
                "gid": index,
                "infoHash": torrent.ready ? torrent.infohash : "",
                "numPieces": torrent.ready ? torrent.pieces.length : 0, //todo
                "numSeeders": 0,
                "pieceLength": torrent.ready ? torrent.pieceLength : 0,
                "status": "active",
                "totalLength": torrent.ready ? torrent.length : 0,
                "uploadLength": torrent.ready ? torrent.uploaded : 0,
                "uploadSpeed": torrent.ready ? torrent.uploadSpeed() : 0

            };
            result.push(info);
        }
        index++;
    });
    return aria2.getReturn(object, result);

    /**
    var retval = {"id":95,"jsonrpc":"2.0","result":[{"bitfield":"0000000000000000000000000000000000" +
        "00000000000000000000000000000000000000000000000000000000000000000000",
        "bittorrent":{"announceList":[["udp:\/\/tracker.yify-torrents.com\/announce"],["udp:\/\/open.demonii.com:1337"],["udp:\/\/exodus.desync.com:6969"],["udp:\/\/tracker.istole.it:80"],["udp:\/\/tracker.publicbt.com:80"],["udp:\/\/tracker.openbittorrent.com:80"],["udp:\/\/tracker.leechers-paradise.org:6969"],["udp:\/\/9.rarbg.com:2710"],["udp:\/\/p4p.arenabg.ch:1337"],["udp:\/\/p4p.arenabg.com:1337"],["udp:\/\/tracker.coppersurfer.tk:6969"]],"comment":"Made using YIFY Bot","creationDate":1444268527,"info":{"name":"Dope (2015)"},"mode":"multi"},
        "completedLength":"0","connections":"0","dir":"D:\\electron\\aria2\\aria2-1.19.0-win-32bit-build1",
        "downloadSpeed":"0",
        "files":[{"completedLength":"0","index":"1","length":"846000994",
            "path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Dope (2015)\/Dope.2015.720p.BluRay.x264.YIFY.mp4",
            "selected":"true","uris":[]},{"completedLength":"0","index":"2","length":"105983",
            "path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Dope (2015)\/WWW.YTS.TO.jpg",
            "selected":"true","uris":[]}],

        "gid":"124ac71c1aa764a7",
        "infoHash":"4bdc86d1c8f1ca0bdef51e88fe84ee6e5ee09f2d",
        "numPieces":"404","numSeeders":"0","pieceLength":"2097152",
        "status":"active","totalLength":"846106977",
        "uploadLength":"0","uploadSpeed":"0"}]}
        */
};
aria2._getBittorrentInfo = function(torrent){
    //"bittorrent":{"announceList":[["udp:\/\/tracker.yify-torrents.com\/announce"],["udp:\/\/open.demonii.com:1337"],["udp:\/\/exodus.desync.com:6969"],["udp:\/\/tracker.istole.it:80"],["udp:\/\/tracker.publicbt.com:80"],["udp:\/\/tracker.openbittorrent.com:80"],["udp:\/\/tracker.leechers-paradise.org:6969"],["udp:\/\/9.rarbg.com:2710"],["udp:\/\/p4p.arenabg.ch:1337"],["udp:\/\/p4p.arenabg.com:1337"],["udp:\/\/tracker.coppersurfer.tk:6969"]],
    // "comment":"Made using YIFY Bot","creationDate":1444268527,
    // "info":{"name":"Dope (2015)"},"mode":"multi"},
    var announceList = [];
   // console.log(torrent.announce);
   // console.log("OK")
    if(!torrent.announce || torrent.announce == undefined){
        return announceList;
    }
    torrent.announce.forEach(function(tracker){
        announceList.push([tracker]);
    })

    return {
        "announceList":announceList,
        "comment":torrent.metadata.comment,
        "creationDate":torrent.metadata.creationDate,
        "info":"",
        "mode":"multi"
    };
}
aria2._getFiles = function(torrent){
    var retval = [];
    var index = 1;
    torrent.files.forEach(function(file){

        retval.push({
                "name":file.name,
               "completedLength":"",
                "length":file.length,
                "index":index,
                "path":file.path,
                "selected":"true",
                "uris":[]
        });
        index++;
    });
    return retval;
};
aria2.getGlobalStat = function(object){
  //  {"id":56,"jsonrpc":"2.0","result":{"downloadSpeed":"0","numActive":"1","numStopped":"1","numStoppedTotal":"1","numWaiting":"0","uploadSpeed":"0"}}
  // console.log(client.downloadSpeed);
    var result = {
        "downloadSpeed":client.downloadSpeed(),
        "uploadSpeed":client.uploadSpeed(),
        "numActive":client.torrents.lenght,
        "numStopped":"0", //todo
        "numStoppedTotal":"0",
        "numWaiting":"0"
    }
    return aria2.getReturn(object, result);
};
aria2.tellWaiting = function(object){
    var result = [];
    return aria2.getReturn(object, result);
};
aria2.tellStopped = function(object){
    //var retval = {"id":11,"jsonrpc":"2.0","result":[{"bitfield":"80","completedLength":"8812","connections":"0","dir":"D:\\electron\\aria2\\aria2-1.19.0-win-32bit-build1","downloadSpeed":"0","errorCode":"0","files":[{"completedLength":"8812","index":"1","length":"8812","path":"D:\/electron\/aria2\/aria2-1.19.0-win-32bit-build1\/Dope (2015) [720p] YIFY - YTS.torrent","selected":"true","uris":[{"status":"used","uri":"http:\/\/yts.to\/torrent\/download\/4BDC86D1C8F1CA0BDEF51E88FE84EE6E5EE09F2D.torrent"},{"status":"waiting","uri":"http:\/\/yts.to\/torrent\/download\/4BDC86D1C8F1CA0BDEF51E88FE84EE6E5EE09F2D.torrent"}]}],"followedBy":["42c47a2a65383c1b"],"gid":"31f6b28e3b534b44","numPieces":"1","pieceLength":"1048576","status":"complete","totalLength":"8812","uploadLength":"0","uploadSpeed":"0"}]}
    var result = [];
    return aria2.getReturn(object, result);
};
aria2.getGlobalOption = function(object){
  //var retval = {"id":25,"jsonrpc":"2.0","result":{"downloadSpeed":"0","numActive":"0","numStopped":"0","numStoppedTotal":"0","numWaiting":"0","uploadSpeed":"0"}};
 var result = {
     "downloadSpeed":"0",
     "numActive":"0",
     "numStopped":"0",
     "numStoppedTotal":"0",
     "numWaiting":"0",
     "uploadSpeed":"0"
 };
  return aria2.getReturn(object, result);
};

aria2.changeGlobalOption = function(object){
    //var retval = {"id":25,"jsonrpc":"2.0","result":{"downloadSpeed":"0","numActive":"0","numStopped":"0","numStoppedTotal":"0","numWaiting":"0","uploadSpeed":"0"}};
    var result = {};
    return aria2.getReturn(object, result);
};
//add
//[{"jsonrpc":"2.0","method":"aria2.addUri","id":186,"params":[["http://yts.to/torrent/download/4BDC86D1C8F1CA0BDEF51E88FE84EE6E5EE09F2D.torrent"],{}]}]
aria2.addUri = function (object){
    var retval = {};
    //object.forEach(function(info){
    object.params.forEach(function(fileUrl){
            var magnetUri = fileUrl[0];
            console.log("client start downloading", fileUrl[0]);
            if(magnetUri) {
                client.add(magnetUri, function (torrent) {
                    console.log('Client is downloading:', torrent.infoHash)
                    //retval.push(this.getReturn(info, magnetUri));

                    aria2.torrents.push({"id":"fileUrl[0]","obj":torrent});
                    retval = aria2.getReturn(object, magnetUri);
                })
            }
     });
   // })
    return aria2.getReturn(object, "OK");;

    //[{"id":186,"jsonrpc":"2.0","result":"48b7d681a69b7cb0"}]
}
aria2.getTorrent = function(torrentId){
    aria2.torrents.forEach(function(torrent){
        if(torrent.id == torrentId)return torrent.obj;
    });
}
//{"jsonrpc":"2.0","method":"aria2.getOption","id":35,"params":[""]}
aria2.getOption = function(object){
    //{"id":15,"jsonrpc":"2.0","result":{"allow-overwrite":"false","allow-piece-length-change":"false","always-resume":"true","async-dns":"true","auto-file-renaming":"true","bt-enable-lpd":"false","bt-force-encryption":"false","bt-hash-check-seed":"true","bt-max-peers":"55","bt-metadata-only":"false","bt-min-crypto-level":"plain","bt-remove-unselected-file":"false","bt-request-peer-speed-limit":"51200","bt-require-crypto":"false","bt-save-metadata":"false","bt-seed-unverified":"false","bt-stop-timeout":"0","bt-tracker-connect-timeout":"60","bt-tracker-interval":"0","bt-tracker-timeout":"60","check-integrity":"false","conditional-get":"false","connect-timeout":"60","continue":"true","dir":"D:\\electron\\aria2\\aria2-1.19.0-win-32bit-build1","dry-run":"false","enable-http-keep-alive":"true","enable-http-pipelining":"false","enable-mmap":"false","enable-peer-exchange":"true","file-allocation":"prealloc","follow-metalink":"true","follow-torrent":"true","force-save":"false","ftp-pasv":"true","ftp-reuse-connection":"true","ftp-type":"binary","hash-check-only":"false","http-accept-gzip":"false","http-auth-challenge":"false","http-no-cache":"false","lowest-speed-limit":"0","max-connection-per-server":"1","max-download-limit":"0","max-file-not-found":"0","max-resume-failure-tries":"0","max-tries":"5","max-upload-limit":"0","metalink-enable-unique-protocol":"true","metalink-preferred-protocol":"none","min-split-size":"20971520","no-file-allocation-limit":"5242880","no-netrc":"false","parameterized-uri":"false","pause-metadata":"false","piece-length":"1048576","proxy-method":"get","realtime-chunk-checksum":"true","remote-time":"false","remove-control-file":"false","retry-wait":"0","reuse-uri":"true","rpc-save-upload-metadata":"true","seed-ratio":"1.0","split":"5","stream-piece-selector":"default","timeout":"60","uri-selector":"feedback","use-head":"false","user-agent":"aria2\/1.19.0"}}
   var result = {"allow-overwrite":"false",
       "allow-piece-length-change":"false",
       "always-resume":"true",
       "async-dns":"true",
       "auto-file-renaming":"true",
       "bt-enable-lpd":"false",
       "bt-force-encryption":"false",
       "bt-hash-check-seed":"true",
       "bt-max-peers":"55","bt-metadata-only":"false",
       "bt-min-crypto-level":"plain","bt-remove-unselected-file":"false",
       "bt-request-peer-speed-limit":"51200","bt-require-crypto":"false",
       "bt-save-metadata":"false","bt-seed-unverified":"false","bt-stop-timeout": "0",
       "bt-tracker-connect-timeout":"60","bt-tracker-interval":"0",
       "bt-tracker-timeout":"60","check-integrity":"false",
       "conditional-get":"false","connect-timeout":"60",
       "continue":"true",
       "dir":"D:\\electron\\aria2\\aria2-1.19.0-win-32bit-build1",
       "dry-run":"false","enable-http-keep-alive":"true","enable-http-pipelining":"false",
       "enable-mmap":"false","enable-peer-exchange":"true","file-allocation":"prealloc",
       "follow-metalink":"true","follow-torrent":"true","force-save":"false","ftp-pasv":"true",
       "ftp-reuse-connection":"true","ftp-type":"binary","hash-check-only":"false",
       "http-accept-gzip":"false","http-auth-challenge":"false","http-no-cache":"false",
       "lowest-speed-limit":"0","max-connection-per-server":"1","max-download-limit":"0",
       "max-file-not-found":"0","max-resume-failure-tries":"0","max-tries":"5",
       "max-upload-limit":"0","metalink-enable-unique-protocol":"true","metalink-preferred-protocol":"none",
       "min-split-size":"20971520","no-file-allocation-limit":"5242880","no-netrc":"false","parameterized-uri":"false",
       "pause-metadata":"false","piece-length":"1048576","proxy-method":"get","realtime-chunk-checksum":"true",
       "remote-time":"false","remove-control-file":"false","retry-wait":"0","reuse-uri":"true",
       "rpc-save-upload-metadata":"true","seed-ratio":"1.0","split":"5","stream-piece-selector":"default",
       "timeout":"60","uri-selector":"feedback","use-head":"false","user-agent":"aria2\/1.19.0"};
        return aria2.getReturn(object, result);
}
//{"jsonrpc":"2.0","method":"aria2.getPeers","id":140,"params":["db9af50f6ca92772"]}
aria2.getPeers = function(object){
   // {"id":140,"jsonrpc":"2.0","result":[{"amChoking":"true",
   // "bitfield":"f5ad18413028a08b0804182c79cc0f302a2881224084c74022063463a004e134d945d8dd4052809140f89bc14e0f837260340e80",
   // "downloadSpeed":"0","ip":"105.210.187.181","peerChoking":"true","peerId":"%2DTR2820%2Dljgkg0zhfcri","port":"51413",
   // "seeder":"false","uploadSpeed":"0"},
   // {"amChoking":"false",
   // "bitfield":"00000000000000000040000008000000000000000000000000000000001000000000000000000000000000000000000000000080",
   // "downloadSpeed":"4735","ip":"58.96.120.29","peerChoking":"false","peerId":"%2DBC0137%2D%88l%0D1%DE%FB%E8C%F9%F6U%23","port":"26193","seeder":"false","uploadSpeed":"24860"},{"amChoking":"true","bitfield":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","downloadSpeed":"0","ip":"180.241.146.58","peerChoking":"true","peerId":"%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00","port":"39824","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80","downloadSpeed":"0","ip":"188.2.12.164","peerChoking":"true","peerId":"%2DUT3450%2D%CA%A0S%8B%89%C7%0B%CE%E3h%EE%CE","port":"64365","seeder":"true","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80","downloadSpeed":"0","ip":"175.138.150.97","peerChoking":"true","peerId":"%2DTB0338%2D30BcFKl%7ESj7Q","port":"49962","seeder":"true","uploadSpeed":"0"},{"amChoking":"true","bitfield":"80000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000","downloadSpeed":"0","ip":"112.209.33.56","peerChoking":"false","peerId":"%2DUT3450%2D%CA%A06%D3t%87%F6%A3%60%AB%3Dm","port":"10378","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"80000008040000000000080040000101000008100000010508248041805443022207604c002bd0da4c05a349b13fdcbfa97dd880","downloadSpeed":"0","ip":"112.210.31.13","peerChoking":"true","peerId":"%2DDE13B0%2DlclOqB76%2A%21H%2E","port":"63897","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80","downloadSpeed":"0","ip":"192.171.32.190","peerChoking":"true","peerId":"%2DUT346B%2D%06%A1%88%9C%EDk%E5%AE%0D%28%99h","port":"35682","seeder":"true","uploadSpeed":"0"},{"amChoking":"true","bitfield":"fa7b6b4e75efdf7b8bfd9bfc38b72f68fb2d7b3794264ecac5913f22fe59d8c599e09df3d0d70465e55e5a244ba018c150926780","downloadSpeed":"0","ip":"27.142.161.235","peerChoking":"true","peerId":"%2DTR2840%2Dlnki778am7en","port":"51413","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","downloadSpeed":"0","ip":"203.214.115.140","peerChoking":"true","peerId":"%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00","port":"6881","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffff3fb36ff8edcf4ae769df9fced1cbb5dfd5aa4a7db9ef7aeeb0dfefafcfbbfedffb7ffefbfffffdf7fffbffffffbfffffff80","downloadSpeed":"0","ip":"110.143.93.41","peerChoking":"true","peerId":"%2DDE242a%2DRe%5FKrBeyLt%29t","port":"52211","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80","downloadSpeed":"0","ip":"27.6.136.194","peerChoking":"true","peerId":"%2DUT3450%2D%CA%A0%E2%0A%D6%A4%CC%EBp%5D%13S","port":"23202","seeder":"true","uploadSpeed":"0"},{"amChoking":"true","bitfield":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","downloadSpeed":"0","ip":"46.176.44.85","peerChoking":"true","peerId":"%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00","port":"15321","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"b4f5d5976a9425275642695c0fc4c023a5cb91a86a21b10d7da4b4dfab27fbba76dfff7dfebfffdbfcfff76fff7fffbffffffe80","downloadSpeed":"0","ip":"60.254.13.160","peerChoking":"true","peerId":"%2DBT7930%2D9%9F%27Qau%ABW%E0q%10%0A","port":"50023","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"f91c884400018142828082c71154c046080528088b7e72508d8936ce17a42b445ff8908823500c38310c0a30a650241218346880","downloadSpeed":"0","ip":"120.60.74.100","peerChoking":"true","peerId":"M7%2D8%2D1%2D%2D%40uD%0F%83%9C%81%D8%EAb%05%9D","port":"17056","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffffddb7faf8ef576243793ebfdcd38bafdeadaf6a399def7abef8ddaf2fef3b7edffb7dfeffffdbfffff7fbffffffffffffff80","downloadSpeed":"0","ip":"121.98.143.167","peerChoking":"true","peerId":"%2DDE13A0%2DEFyTyghv%7EmLN","port":"49413","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80","downloadSpeed":"0","ip":"135.0.49.214","peerChoking":"true","peerId":"%2DDE13B0%2DFNlJrQ4l%2DFZK","port":"50186","seeder":"true","uploadSpeed":"0"},{"amChoking":"true","bitfield":"80000000000000000000000000000000000000000000000000100000000000000000080000000020000140000000000000000080","downloadSpeed":"0","ip":"197.89.191.86","peerChoking":"true","peerId":"%2DUT3450%2D%CA%A0%FBK%E6%C7%93z%09u%C9m","port":"59447","seeder":"false","uploadSpeed":"0"},{"amChoking":"true","bitfield":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","downloadSpeed":"0","ip":"128.127.204.198","peerChoking":"true","peerId":"%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00","port":"23987","seeder":"false","uploadSpeed":"0"}]}
    var torrent = client.torrents[object.params[0]];
    //console.log(torrent.swarm.wires);
    var result = [];
    torrent.swarm.wires.forEach(function(wire){
       // wire.down
        result.push({
            "downloadSpeed":wire.downloadSpeed(),
            "uploadSpeed":wire.uploadSpeed(),
            "downloaded":wire.downloaded,
            "uploaded":wire.uploaded,
            "amChoking":wire.amChoking,
            "peerChoking":wire.peerChoking,
            "ip":wire.remoteAddress,
            "port":wire.remotePort,
            "seeder":wire.isSeeder,
            "peerId":wire.peerId.toString("hex"),
            "ut_metadata":wire.peerPieces.BitField.buffer.toString("hex")
        });
    });
    return aria2.getReturn(object, result);
}
//pause
 //[{"jsonrpc":"2.0","method":"aria2.pause","id":925,"params":["42c47a2a65383c1b"]}]
aria2.pause  = function(object){
    var torrent = client.torrents[object.params[0]];
    torrent.destroy();
    return aria2.getReturn(object, object.params[0]);
  //  [{"id":925,"jsonrpc":"2.0","result":"42c47a2a65383c1b"}]

}
//start
// [{"jsonrpc":"2.0","method":"aria2.unpause","id":1019,"params":["42c47a2a65383c1b"]}]
aria2.unpause = function(object){

    //[{"id":1019,"jsonrpc":"2.0","result":"42c47a2a65383c1b"}]
}
aria2.getReturn = function(object, result){
    var retval = {
        "id":object.id,
        "jsonrpc":object.jsonrpc,
        "result":result
    };
    return retval;
}